package com.pignest.hotel.spring;

import java.util.ArrayList;
import java.util.List;

import com.pignest.hotel.beans.Role;
import com.pignest.hotel.beans.User;
import com.pignest.hotel.beans.Dictionary;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.beans.RoomType;
import com.pignest.hotel.service.AuditService;
import com.pignest.hotel.service.DictionaryService;
import com.pignest.hotel.service.RoomService;
import com.pignest.hotel.service.UserService;
import com.pignest.hotel.system.Config;
import com.pignest.hotel.system.Global;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.system.Permission;

/** 初始化系统 */
public class SystemInitialize {

	private RoomService roomService;
	private DictionaryService dictionaryService;
	private UserService userService;
	private UserService roleService;
	private AuditService  auditService;
	
	public String initRoomTypes() {
		List<RoomType> types = roomService.listRoomTypes();
		if (types != null && !types.isEmpty())
			return "已经定义了房间类型";
		try {
			String[] names = {"经济间", "标准间", "单人间", "豪华双人间", "豪华单人间", "总统套房"};
			String[][] colors = Global.getColors();
			for (int i = 0; i < names.length; i++) {
				RoomType type = new RoomType(names[i], colors[i % colors.length][1], i % 2 + 1, 150 + i * 10, null);
				roomService.addRoomType(type);
			}
		} catch (Throwable e) {
			return "异常:" + e;
		}
		return null;
	}
	
	public String initRooms() {
		try {
			List<RoomType> types = roomService.listRoomTypes();
			if (types == null || types.isEmpty())
				return "未定义类型";
			List<Room> rooms = roomService.listAllRooms();
			List<String> floors = Hotel.listFloors();
			if (floors == null || floors.isEmpty())
				return "楼层未定义";
			if (rooms != null && !rooms.isEmpty())
				return "已经有定义的房间";
			int roomCount = types.size();
			int start = 5001;
			for (int i = 0; i < 3; i++) {
				start += 1000 * i;
				String floor = floors.get(i % floors.size());
				for (int j = 0; j < 20; j++) {
					roomService.addRoom(String.valueOf(start + j), types.get(j % roomCount).getName(), floor);
				}
			}
		} catch (Throwable e) {
			return "异常:" + e;
		}
		return null;
	}
	
	public String initFloors() {
		try {
			List<String> floors = Hotel.listFloors();
			if (floors != null && !floors.isEmpty())
				return "已经定义了楼层";
			dictionaryService.addDictionary(new Dictionary("floor_1", "1楼"));
			dictionaryService.addDictionary(new Dictionary("floor_2", "2楼"));
			dictionaryService.addDictionary(new Dictionary("floor_3", "3楼"));
			dictionaryService.addDictionary(new Dictionary("floor_4", "4楼"));
			dictionaryService.addDictionary(new Dictionary("floor_5", "5楼"));
			dictionaryService.addDictionary(new Dictionary("floor_6", "6楼"));
			dictionaryService.addDictionary(new Dictionary("floor_7", "7楼"));
			dictionaryService.addDictionary(new Dictionary("floor_8", "8楼"));
		} catch (Throwable e) {
			return "异常:" + e;
		}
		return null;
	}
	
	public String initRoles() {
		try {
			List<Role> roles = roleService.listAll(Role.class);
			if (roles != null && !roles.isEmpty())
				return "角色已经初始化";
			Permission[] ps = Permission.values();
			for (int i = 0; i < ps.length; i++) {
				roleService.insert(new Role(ps[i].getDisplayName(), ps[i]));
			}
			roleService.insert(new Role("前台", Permission.BOOKING, Permission.RECEPTION));
			roleService.insert(new Role("经理", Permission.ACCOUNT_MANAGER, Permission.MEMBER_MANAGER, Permission.REPORT));
			roleService.insert(new Role("管理员", Permission.values()));
		} catch (Throwable e) {
			e.printStackTrace();
			return "异常:" + e;
		}
		
		return null;
	}
	
	public String initUsers() {
		try {
			String superUser = Config.getSuperUserWorkNumber();
			User user = userService.getUserByWorkNo(superUser);
			if (user == null) {
				user = new User("超级用户", superUser, "1", "6756758786");
				user.setReserve(true);
				userService.insert(user);
			}
			List<User> users = userService.listAllUsers();
			if (users != null && !users.isEmpty())
				return "用户已经初始化";
			userService.insert(new User("管理员", "123", "456", "789"));
			userService.insert(new User("自助终端1", "ss1", "ss1", "12345"));
			userService.insert(new User("自助终端2", "ss2", "ss2", "12346"));
			userService.insert(new User("前台1", "qc1", "qc1", "20001"));
			userService.insert(new User("前台2", "qc2", "qc2", "20002"));
			userService.insert(new User("值班经理1", "jl1", "jl1", "30001"));
			userService.insert(new User("值班经理2", "jl2", "jl2", "30002"));
		} catch (Throwable e) {
			return "异常:" + e;
		}
		
		return null;
	}
	/*清空业务
	 * 1、清空结账表 tb_close_accounts
	 * 2、清空账务表 tb_accounts
	 * 3、清空物品借用记录 TB_LIVE_GOODS_BORROW
	 * 4、清空入住详细信息 TB_LIVE_SPECIFICS
	 * 5、清空入住客人 TB_LIVING_CUSTOM
	 * 6、预定排房 TB_BOOKING_ROOM
	 * 7、预定 TB_BOOKING
	 * 8、房间
	 * 
	 * 9、清空入住
	 * **/
	public String clearBusiness()
	{
		try{
			
			return null;
		}catch(Exception e){
			e.printStackTrace();
			return "异常："+e.getMessage();
		}
	}
	public String initAll() {
		List<String> errors = new ArrayList<String>();
		String e = initFloors();
		if (e != null)
			errors.add(e);
		e = initRoomTypes();
		if (e != null)
			errors.add(e);
		e = initRooms();
		if (e != null)
			errors.add(e);
		e = initRoles();
		if (e != null)
			errors.add(e);
		e = initUsers();
		if (e != null)
			errors.add(e);
		return errors.size() > 0 ? errors.toString() : null;
	}
	public String auditLiving()
	{
		try{
			auditService.execCalculateCurrentAccounts();
		}catch(Throwable e){
			e.printStackTrace();
			return "异常："+e;
		}
		return null;
	}
	
	public void setRoomService(RoomService roomService) {
		this.roomService = roomService;
	}
	
	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public void setRoleService(UserService roleService) {
		this.roleService = roleService;
	}

	public void setAuditService(AuditService auditService) {
		this.auditService = auditService;
	}
	
}
